<?php

namespace Modules\AdminPermission\Http\Controllers;


use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Controller;
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;

class AdminPermissionController extends Controller
{
    use AuthorizesRequests, ValidatesRequests;

    /**
     *
     * User: long2ge
     * Date: 2023/12/28 9:54 AM
     * Describe:
     * @return \Illuminate\Http\Response
     */
    public function noContent()
    {
        return response()->noContent();
    }

    public function responseArray($object) : JsonResponse
    {
        return response()->json($object);
    }

    public function responseItem($object) : JsonResponse
    {
        $response = (new Manager())
            ->createData(new Item($object))
            ->toArray();

        return response()->json($response['data']);
    }

    public function responseCollection($object, array $meta = []) : JsonResponse
    {
        $resource = (new Collection($object))->setMeta($meta);

        $response = (new Manager())->createData($resource)->toArray();

        return response()->json($response);
    }

    public function responsePaginate(LengthAwarePaginator $paginator, array $meta = []) : JsonResponse
    {
        $resource = (new Collection($paginator->getCollection()))
            ->setPaginator(new IlluminatePaginatorAdapter($paginator))
            ->setMeta($meta);

        $response = (new Manager())->createData($resource)->toArray();

        return response()->json($response);
    }

    public function validateWithArray(array $params, array $rules) {
        return $this->getValidationFactory()
            ->make($params, $rules)
            ->validate();
    }
}
